package com.cc8w.api.controller;



import com.cc8w.api.service.GoodsService;
import com.cc8w.entity.CategoryEntity;
import com.cc8w.entity.GoodsAttrEntity;
import com.cc8w.entity.GoodsEntity;
import com.cc8w.entity.ModelsEntity;
import com.cc8w.home.controller.BaseController;
import com.cc8w.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Controller(value="apiIndexController")
@RequestMapping("/api")
public class IndexController extends BaseController {

    @Autowired
    private GoodsService goodsService;


    /**
     * 首页数据
     * @return
     */
    @RequestMapping("index")
    @ResponseBody()
    public ResultData index(){
        //1.获得分类
        //2.获得产品
        //3.获得轮播(可以是静态数据)
        //4.获得优惠产品
        List<CategoryEntity> goodCat = goodsService.getGoodsCat();
        List<ModelsEntity> models = goodsService.getModels();
        List<GoodsEntity> goods = goodsService.getGoods();
        List<GoodsEntity> discountGoods = goodsService.getDiscountGoods();

        System.out.println(models);

        //组装数据
        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("goodCat",goodCat);
        hashMap.put("models",models);
        hashMap.put("goods",goods);
        hashMap.put("discountGoods",discountGoods);

        //返回数据
        return new ResultData(0,"success",hashMap);
    }
    /**
     * 获得指定分类产品
     * @return
     */
    @RequestMapping("getgoodsbycat")
    @ResponseBody()
    public ResultData getGoodsByCat(){

        List<CategoryEntity> goodCat = goodsService.getGoodsCat();
        //组装数据
        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("goodCat",goodCat);

        //返回数据
        return new ResultData(0,"success",hashMap);
    }
    /**
     * 获得商品列表
     */
    @RequestMapping("getgoodslistbycatid")
    @ResponseBody()
    public ResultData getGoodsListByCatId(@RequestParam(value="catId",defaultValue = "0") int catId){


        List<GoodsEntity> goodsList = goodsService.getGoodsListByCatId(catId);
        //组装数据
        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("goodsList",goodsList);


        //返回数据
        return new ResultData(0,"success",hashMap);
    }


    /**
     * 产品详情
     */
    @RequestMapping("getgoodsdetail")
    @ResponseBody()
    public ResultData getGoodsDetail( @RequestParam(value="goods_id",defaultValue = "") int goodsId){

        //1.根据产品id查产品
        GoodsEntity goodsInfo =  goodsService.getGoodsDetails(goodsId);
        System.out.println(goodsInfo);

        //2.查询产品对应的属性和其属性值
        //2.1 通过产品id查对应的属性
        //2.2 通过对应的属性id查相应的属性数组
        List<GoodsAttrEntity> goodsAttr  = goodsService.getGoodsAttr(goodsId);
        System.out.println(goodsAttr);



        //组装数据
        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("goodsInfo",goodsInfo);
        hashMap.put("goodsAttr",goodsAttr);


        //返回数据
        return new ResultData(0,"success",hashMap);
    }

    /**
     * 获取产品某种属性组合的价格
     */
    @RequestMapping("getgoodsprice")
    @ResponseBody()
    public ResultData getGoodsPrice(@RequestParam(value="goods_id",defaultValue = "") int goodsId,
                                @RequestParam(value="goods_sn",defaultValue = "") String goodsSn){

        BigDecimal price = goodsService.getGoodsPrice(goodsId,goodsSn);

        //组装数据
        HashMap<String, Object> hashMap = new HashMap<String,Object>();
        hashMap.put("price",price);

        //返回数据
        return new ResultData(0,"success",hashMap);
    }

    /**
     *
     */


}
